package group.sicher.lock.brain.lock;

import group.sicher.lock.Exception.MarsKeyBuilderException;
import group.sicher.lock.annotation.MarsKey;
import group.sicher.lock.brain.MarsLockKey;

import java.lang.reflect.Method;

/**
 * @description: 参数上的锁注解
 * @author: aron
 * @date: 2019-08-05 16:59
 */
public class MarsKeyParameterLock extends MarsKeyLockAbstractor {

    public MarsKeyParameterLock(String className, String methodName, Method realMethod, Object[] args) {
        super(className, methodName, realMethod, args);
    }

    @Override
    public MarsLockKey.Builder builder() throws MarsKeyBuilderException {
        MarsLockKey.Builder keyBuilder = MarsLockKey.instance();
        for (int i = 0; i < realMethod.getParameters().length; i++) {
            if (realMethod.getParameters()[i].isAnnotationPresent(MarsKey.class)) {
                keyBuilder.setKey(wrapKeyTag(args[i].toString()));
            }
        }
        return keyBuilder;
    }
}
